package cn.edu.cqytxy.dao;

import cn.edu.cqytxy.entity.Aircraft;
import cn.edu.cqytxy.util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用来映射对接实体类和数据库表
 *
 * @author yinjw
 */
public class AircraftDao {

    /**
     * 添加飞机
     * @param aircraft  被添加的飞机
     * @return  影响行数
     */
    public int insertAircraft(Aircraft aircraft) {

        // 获取数据库连接
        Connection connection = JdbcUtils.getConnection();

        // 组装sql语句
        StringBuilder sb = new StringBuilder("INSERT INTO aircraft(`name`,num," +
                "altitude,speed) " +
                "value(?,?,?,?)");

        System.out.println("添加军用飞机sql：" + sb);
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sb.toString());

            preparedStatement.setString(1, aircraft.getName());
            preparedStatement.setInt(2, aircraft.getNum());
            preparedStatement.setInt(3, aircraft.getAltitude());
            preparedStatement.setInt(4, aircraft.getSpeed());

            // insert delete update 都是用executeUpdate()
            return preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    /**
     * 查询所有飞机
     * @return  所有飞机
     */
    public List<Aircraft> selectAll() {

        List<Aircraft> aircrafts = new ArrayList<Aircraft>();

        // 获取数据库连接
        Connection connection = JdbcUtils.getConnection();

        // 组装执行的sql
        String sql = "select * from aircraft";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            // 获取查询结果并遍历赋值
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                // 根据战斗机类型匹配不同类型的飞机
                Aircraft aircraft = new Aircraft();
                aircraft.setName(resultSet.getString("name"));
                aircraft.setNum(resultSet.getInt("num"));
                aircraft.setAltitude(resultSet.getInt("altitude"));
                aircraft.setSpeed(resultSet.getInt("speed"));
                aircraft.setStealth(resultSet.getString("stealth"));
                aircraft.setRadar(resultSet.getString("radar"));
                aircrafts.add(aircraft);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return aircrafts;
        } finally {
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (resultSet != null) {
                    resultSet.close();
                }
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return aircrafts;
    }
}
